# Maintainer: Hoang Nguyen <folliekazetani@protonmail.com>
pkgname=linkerd-cli
pkgver=2.14.7
pkgrel=0
pkgdesc="CLI utility for Linkerd service mesh"
url="https://linkerd.io/"
arch="all"
license="Apache-2.0"
makedepends="go"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="linkerd2-$pkgver.tar.gz::https://github.com/linkerd/linkerd2/archive/refs/tags/stable-$pkgver.tar.gz"
builddir="$srcdir/linkerd2-stable-$pkgver"

export CGO_ENABLED=0
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare

	# https://github.com/linkerd/linkerd2/blob/main/pkg/charts/static/helpers.go
	# runtime.Caller(0) returns the module path (not the absolute file system path) when '-trimpath' is presented
	# => FATA[0000]: github.com/linkerd/linkerd2/charts: no such file or directory
	local GOFLAGS="${GOFLAGS/-trimpath/}"

	for target in pkg/charts jaeger multicluster viz; do
		go generate ./"$target"/static
	done
}

build() {
	go build -v \
		-tags prod \
		-o ./linkerd \
		-ldflags "-X github.com/linkerd/linkerd2/pkg/version.Version=stable-$pkgver" \
		./cli

	for shell in bash fish zsh; do
		./linkerd completion $shell > linkerd.$shell
	done
}

check() {
	# Static template files are referenced the same way as they were in `go generate` commands
	GOFLAGS="${GOFLAGS/-trimpath/}" go test ./...
}

package() {
	install -Dm755 linkerd -t "$pkgdir"/usr/bin/

	install -Dm644 linkerd.bash \
		"$pkgdir"/usr/share/bash-completion/completions/linkerd
	install -Dm644 linkerd.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/linkerd.fish
	install -Dm644 linkerd.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_linkerd
}

sha512sums="
2b49c890ae3cf75492872634250b5a42a26aa1a5000daf7ffe26091e8e45a57fcf5eb17c888ef7af281f5c441e1641ee4b5f28cc3772bfb687e9398709028c76  linkerd2-2.14.7.tar.gz
"
